/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
    public:
        TreeNode* sortedListToBST(ListNode* head) {

            if (!head)
                return NULL;

            if (!head->next)
                return new TreeNode(head->val);

            ListNode *p = head;
            ListNode *q = head;
            while(q && q->next) {
                p = p->next;
                q = q->next->next;
            }
            q = head;
            while(q->next && q->next!=p) {
                q = q->next;
            }
            q->next = NULL;

            TreeNode *root = new TreeNode(p->val);
            root->left = sortedListToBST(head);
            root->right = sortedListToBST(p->next);
            return root;
        }
};


